package com.example.rabbitmqspringboot.controller;

import com.example.rabbitmqspringboot.config.DelayedQueueConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author zfj
 * @date 2022/4/26 -10:02
 */
@Slf4j
@RequestMapping("/ttl")
@RestController
public class SendMsgController {

	@Autowired
	private RabbitTemplate rabbitTemplate;

	//开始发消息
	@GetMapping("/sendMsg/{message}")
	public void sendMsg(@PathVariable String message){
		log.info("当前时间：{},发送一条信息给两个 TTL 队列:{}", new Date(), message);
		rabbitTemplate.convertAndSend("X", "XA", "消息来自 ttl 为 10S 的队列: "+message);
		rabbitTemplate.convertAndSend("X", "XB", "消息来自 ttl 为 40S 的队列: "+message);
	}


	@GetMapping("/sendExpirationMsg/{message}/{ttlTime}")
	public void sendMsg(@PathVariable String message,@PathVariable String ttlTime){
		log.info("当前时间：{},发送一条时长{}毫秒 TTL 信息给队列 C:{}", new Date(),ttlTime, message);
		rabbitTemplate.convertAndSend("X","XC",message,message1 -> {
			message1.getMessageProperties().setExpiration(ttlTime);
			return message1;
		});
	}

	@GetMapping("/sendDelayMsg/{message}/{delayTime}")
	public void sendMsg(@PathVariable String message,@PathVariable Integer delayTime){
		log.info("当前时间：{},发送一条时长{}毫秒 信息给延迟队列队列 delayedQueue:{}", new Date(),delayTime, message);
		rabbitTemplate.convertAndSend(DelayedQueueConfig.DELAYED_EXCHANGE_NAME,DelayedQueueConfig.DELAYED_ROUTING_KEY,message, message1 -> {
			message1.getMessageProperties().setDelay(delayTime);
			return message1;
		});
	}

	//开始发消息，测试确认

}
